package com.lagou.edu.controller;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author wyx
 * @Date 2020/1/23 9:26
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<Resume> queryAll() throws Exception {

        List<Resume> list= resumeService.findAll();

        return list;

    }

    @RequestMapping("/ResumeList")
    public ModelAndView ResumeList() throws Exception {

        List<Resume> resumeList= resumeService.findAll();

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resumeList", JSON.toJSONString(resumeList));
        modelAndView.addObject("date",date);

        modelAndView.setViewName("resumeList");
        return modelAndView;
    }


    @RequestMapping("/addSession")
    public void SaveSession(HttpServletRequest request){

        HttpSession session = request.getSession();
//session默认过期时间是30分钟，可以用下面的方法设置。
        session.setMaxInactiveInterval(60); //单位是秒。
//添加一个session。
        session.setAttribute("username", "admin"); //设置一个名为name，值为value的session；
        session.setAttribute("password", "admin"); //设置一个名为name，值为value的session；

//获取指定的session值。
        String username= (String)session.getAttribute("username");
        String password= (String)session.getAttribute("password");

//获取sessionId。
        String sessionId = session.getId();

        System.out.println(username+" and "+password+" sessionId "+sessionId);

    }


    @RequestMapping("/login")
    public String login() {

        return "login";
    }

    @RequestMapping("/deleteById")
    public @ResponseBody String deleteById(@RequestParam("id") long id) {// @RequestBody long id
        System.out.println("开始删除");
        resumeService.deleteById(id);
        return "刪除成功";
    }

    @ResponseBody
    @RequestMapping("/save")
    public  String save(@RequestBody Resume resume) {
        System.out.println("开始保存");
        resumeService.addResume(resume);
        System.out.println("保存成功");
        return "保存成功";
    }

}